# 我是注释，单行注释
'''
我也是注释，多行注释
'''
"""
我还是注释，多行注释
"""


## 节点类
class TreeNode:
    def __init__(self, value=0, parent=None, left=None, right=None) -> None:
        self.value = value
        self.parent = parent
        self.left = left
        self.right = right

## 前序遍历，根节点 -> 左子树 -> 右子树

### 递归实现
def pre_order_traversal_recursive(root):
    if root is None:
        return []
    return [root.value] + pre_order_traversal_recursive(root.left) + pre_order_traversal_recursive(root.right)

### 迭代实现
def pre_order_traversal_iterative(root):
    if root is None:
        return []
    stack, output = [root], []
    while stack:
        node = stack.pop()
        if node:
            output.append(node.value)
            if node.right:
                stack.append(node.right)
            if node.left:
                stack.append(node.left)
    return output

## 中序遍历，左子树 -> 根节点 -> 右子树。

### 递归实现
def in_order_traversal_recursive(root):
    if root is None:
        return []
    return in_order_traversal_recursive(root.left) + [root.value] + in_order_traversal_recursive(root.right)

### 迭代实现
def in_order_traversal_iterative(root):
    stack, output = [], []
    current = root
    while stack or current:
        while current:
            stack.append(current)
            current = current.left
        current = stack.pop()
        output.append(current.value)
        current = current.right
    return output

## 后序遍历，左子树 -> 右子树 -> 根节点

### 递归实现
def post_order_traversal_recursive(root):
    if root is None:
        return []
    return post_order_traversal_recursive(root.left) + post_order_traversal_recursive(root.right) + [root.value]

### 迭代实现
def post_order_traversal_iterative(root):
    if root is None:
        return []
    stack, output = [root], []
    while stack:
        node = stack.pop()
        if node:
            output.append(node.value)
            if node.left:
                stack.append(node.left)
            if node.right:
                stack.append(node.right)
    return output[::-1]  # Reverse the process to get the correct order

## 层次遍历，按层次从上到下，从左到右遍历。

### 迭代实现
from collections import deque

def level_order_traversal(root):
    if root is None:
        return []
    queue, output = deque([root]), []
    while queue:
        node = queue.popleft()
        output.append(node.value)
        if node.left:
            queue.append(node.left)
        if node.right:
            queue.append(node.right)
    return output

## 示例

# 构建示例二叉树
#        1
#       / \
#      2   3
#     / \ / \
#    4  5 6  7

root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)
root.right.left = TreeNode(6)
root.right.right = TreeNode(7)

# 前序遍历
print("pre order traversal (Recursive):", pre_order_traversal_recursive(root))
print("pre order traversal (Iterative):", pre_order_traversal_iterative(root))

# 中序遍历
print("in order traversal (Recursive):", in_order_traversal_recursive(root))
print("in order traversal (Iterative):", in_order_traversal_iterative(root))

# 后序遍历
print("post order traversal (Recursive):", post_order_traversal_recursive(root))
print("post order traversal (Iterative):", post_order_traversal_iterative(root))

# 层次遍历
print("level order traversal:", level_order_traversal(root))